package com.kmx.demo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author spring at 2016年1月22日 下午6:53:22
 */
public class K2FileUtils  {

	private static String separator = File.separator;
	
	public static void main(String[] args) {
		writeContentsToFile("/lzx/test1", "test1");
		writeContentsToFile("/lzx/test1", "test2");
		List<File> files = listFiles("../hydra-k2platform-v010-test/data");
		for (File file : files) {
			System.out.println(file.getAbsolutePath());
		}
	}
	
	/**
	 * backup file
	 * @param fileName
	 * @return
	 */
	public static String backupFile(String fileName){			
		File file = new File(fileName);
		if(file.exists()){		
			String time = DateUtils.getDate(new Date(), "yyyy.MM.dd_HH.mm.ss.SSS");			
			String dest = file.getAbsolutePath() + "_bakAt_" + time;
			
			file.renameTo(new File(dest));
			
			return dest;
		}
		return fileName;
	}

	public static String filePath(String path) {
		return path.endsWith(separator) ? path : path + separator;
	}

	/**@author spring*/
	public static boolean deleteCascade(File file) {
		boolean flag = true;
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else {
				File[] files = file.listFiles();
				for (File nextLevel : files) {
					deleteCascade(nextLevel);
				}
				flag = file.delete();
			}
		}
		return flag;
	}

	/**@author spring*/
	public static boolean deleteCascade(String path) {
		return deleteCascade(new File(path));
	}
	
	private static List<File> listFiles(List<File> lists,File file){
		if (file.exists()) {
			if (file.isFile()) {
				lists.add(file);
			}else{
				File[] files = file.listFiles();
				for (File f : files) {
					listFiles(lists,f);
				}
			}
			return lists;
		}
		return lists;
	}
	
	/**
	 * 遍历目录或文件,返回所有的文件列表
	 * @author spring
	 * */
	public static List<File> listFiles(File file){
		if (file.exists()) {
			List<File> lists = new ArrayList<>();
			if (file.isFile()) {
				lists.add(file);
			}else{
				lists = listFiles(lists, file);
			}
			return lists;
		}
		return null;
	}
	
	/**
	 * 遍历目录或文件,返回所有的文件列表
	 * @author spring
	 * */
	public static List<File> listFiles(String pathname){
		return listFiles(new File(pathname));
	}

//	public static void copyFileToDirectory(String path, String destDir) throws IOException {
//		org.apache.commons.io.FileUtils.copyFileToDirectory(new File(path), new File(destDir));
//	}
//
//	public static void copyFile(File srcFile, File destFile) throws IOException {
//		org.apache.commons.io.FileUtils.copyFile(srcFile, destFile);
//	}

	public static boolean backupFileToCurrent(String dirPath) {
		File dir = new File(dirPath);
		if (dir.exists()) {
			String time = DateUtils.getDate(new Date(), "yyyy.MM.dd_HH.mm.ss.SSS");
			return dir.renameTo(new File(dir.getAbsolutePath() + "_bakAt_" + time));
		} 
		return false;
	}

	public static String backupAndRebuildDir(String dirPath) {
		File dir = new File(dirPath);
		if (!dir.exists()) {
			mkdirs(dirPath);
			return dirPath;
		}
		backupFileToCurrent(dirPath);
		mkdirs(dirPath);
		return dirPath;
	}

	public static void deleteAndRebuildDir(String dirPath) {
		File file = new File(dirPath);
		deleteCascade(file);
		file.mkdirs();
	}

	public static void mkdirs(String dirPath) {
		File file = new File(dirPath);
		if (!file.exists() || !file.isDirectory()) {
			file.mkdirs();
		}
	}

	public static String getFileContent(File file) throws IOException {
		InputStream inputStream = new FileInputStream(file);
		StringBuffer buffer = new StringBuffer();
		byte[] buff = new byte[inputStream.available()];
		int byteRead = 0;
		while ((byteRead = inputStream.read(buff)) != -1) {
			buffer.append(new String(buff, 0, byteRead));
		}
		inputStream.close();
		return buffer.toString();
	}

	public static void writeContentsToFile(File file, String content, boolean appendMode) {
		FileWriter writer = null;
		try {
			writer = new FileWriter(file, appendMode);
			writer.write(content);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeContentsToFile(String path, String content, boolean appendMode) {
		writeContentsToFile(new File(path),content,appendMode);
	}
	
	
	public static void writeContentsToFile(String path, String content) {
		writeContentsToFile(path,content,false);
//		FileOutputStream outputStream = null;
//		BufferedOutputStream buffer = null;
//		try {
//			outputStream = new FileOutputStream(new File(path));
//			buffer = new BufferedOutputStream(outputStream);
//			buffer.write(content.getBytes());
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		try {
//			buffer.flush();
//			buffer.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
	
	public static void writeListsToFile(File file,List<?> lists) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (Object obj : lists) {
			writer.write(obj.toString() + "\n");
		}
		writer.close();
	}

	/**
	 * 
	 * @param filePath
	 * @param fileName
	 * @param content
	 * @param appendMode
	 */
	public static void writeContentsToFile(String filePath, String fileName, String content, boolean appendMode) {
		filePath = filePath(filePath);
		File path = new File(filePath);
		if (!path.exists()) {
			path.mkdirs();
		}
		FileWriter writer = null;
		try {
			writer = new FileWriter(filePath + fileName, appendMode);
			writer.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 
	 * @param writer
	 * @param filePath
	 * @param fileName
	 * @param content
	 * @param appendMode
	 */
	public static void writeContentsToFile(FileWriter writer, String filePath, String fileName, String content,
			boolean appendMode) {
		filePath = filePath(filePath);
		File path = new File(filePath);
		if (!path.exists()) {
			path.mkdirs();
		}
		try {
			writer = new FileWriter(filePath + fileName, appendMode);
			writer.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * create a file
	 * @author lzx
	 * @param path
	 * @param name
	 */
	public static void createFile(String path, String name) {
		try {
			File file = new File(filePath(path) + name);
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}

	}

	/**
	 * remove a file
	 * @author lzx
	 * @param path
	 * @param name
	 */
	public static void removeFile(String path, String name) {
		try {
			File file = new File(filePath(path) + name);
			if (file.exists()) {
				file.delete();
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public static void splitFileByLineNum(String filePath, int num, String storagePath) {

	}
	
	/**
	 * get file content as string
	 * @return
	 */
	public static String getFileString(String filepath) {
		String fileStr;
		byte[] strBuffer = null;
		int flen = 0;
		File file = new File(filepath);
		try {
			InputStream in = new FileInputStream(file);
			flen = (int) file.length();
			strBuffer = new byte[flen];
			in.read(strBuffer, 0, flen);

			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		fileStr = new String(strBuffer);
		return fileStr;
	}

}